﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DRSM
{
    public partial class LoginPage : Form
    {
        private bool newUserName = false;
        private bool newUserID = false;
        private bool newPassword = false;
        private bool verifyPassword = false;
        private bool notSimilarUserID = false;
        
       
        
        public LoginPage()
        {
            
            InitializeComponent();
            pnlRegister.Hide();
            pnlLoginEmpty.Show();
            this.lbltime.Text = DateTime.Now.ToString();
            this.timer1.Start();
        }

        private void btnNewRegister_Click(object sender, EventArgs e)
        {
            user newUser = new user(tbxNewName.Text, tbxNewUserID.Text, tbxNewPassword.Text);
            AllFormClass.User.Add(newUser);
            MessageBox.Show("Registration Successful!");
            tbxNewName.Clear();
            tbxNewPassword.Clear();
            tbxNewPasswordComfirmation.Clear();
            tbxNewUserID.Clear();
           
        }

        private void btnResetRegister_Click(object sender, EventArgs e)
        {
            tbxNewName.Clear();
            tbxNewPassword.Clear();
            tbxNewPasswordComfirmation.Clear();
            tbxNewUserID.Clear();
        }

        private void tbxNewName_TextChanged(object sender, EventArgs e)
        {
            tbxNewName.Focus();
            newUserName = tbxNewName.Text != "";
            if (newUserName && newUserID && newPassword && notSimilarUserID && verifyPassword)
            {
                this.btnNewRegister.Enabled = true;
            }
            else
            {
                this.btnNewRegister.Enabled = false;
            }

        }

        private void tbxNewUserID_TextChanged(object sender, EventArgs e)
        {
            newUserID = tbxNewUserID.Text != "";
            if (newUserName && newUserID && newPassword && notSimilarUserID && verifyPassword)
            {
                this.btnNewRegister.Enabled = true;
            }
            else
            {
                this.btnNewRegister.Enabled = false;
            }
        }

        private void tbxNewPassword_TextChanged(object sender, EventArgs e)
        {
            newPassword = tbxNewPassword.Text != "";
            if (newUserName && newUserID && newPassword && notSimilarUserID && verifyPassword)
            {
                this.btnNewRegister.Enabled = true;
            }
            else
            {
                this.btnNewRegister.Enabled = false;
            }
        }

        private void tbxNewPasswordComfirmation_TextChanged(object sender, EventArgs e)
        {
            verifyPassword = tbxNewPasswordComfirmation.Text != "";
            if (newUserName && newUserID && newPassword && notSimilarUserID && verifyPassword)
            {
                this.btnNewRegister.Enabled = true;
            }
            else
            {
                this.btnNewRegister.Enabled = false;
            }

        }

        private void tbxNewName_Leave(object sender, EventArgs e)
        {

        }

        private void tbxNewUserID_Leave(object sender, EventArgs e)
        {
            if (AllFormClass.User.Count > 0)
            {
                for (int i = 0; i < AllFormClass.User.Count; i++)
                {
                    user u = (user)AllFormClass.User[i];
                    if (tbxNewUserID.Text.Trim() == u.UserID.ToString())
                    {
                        notSimilarUserID = false;
                        similarUserID.SetError(tbxNewUserID, "UserID already exist!");
                    }
                    else if (tbxNewUserID.Text.Trim() != u.UserID.ToString())
                    {
                        notSimilarUserID = true;
                        similarUserID.Clear();
                    }
                }
            }
            else
            {
                notSimilarUserID = true;
            }
            
            if (newUserName && newUserID && newPassword && notSimilarUserID && verifyPassword)
            {
                this.btnNewRegister.Enabled = true;
            }
            else
            {
                this.btnNewRegister.Enabled = false;
            }
        }

        private void tbxNewPasswordComfirmation_Leave(object sender, EventArgs e)
        {
            if (tbxNewPasswordComfirmation.Text.Trim() != tbxNewPassword.Text.Trim())
            {
                verifyPassword = false;
                passwordDontMatch.SetError(tbxNewPasswordComfirmation, "Password mismatch!");
            }
            else if (tbxNewPasswordComfirmation.Text.Trim() == tbxNewPassword.Text.Trim())
            {
                verifyPassword = true;
                passwordDontMatch.Clear();
            }

            if (newUserName && newUserID && newPassword && notSimilarUserID && verifyPassword)
            {
                this.btnNewRegister.Enabled = true;
            }
            else
            {
                this.btnNewRegister.Enabled = false;
            }
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            string username = "";
            string userID = tbxUserID.Text.Trim();
            string password = tbxPass.Text.Trim();

            user login = new user(null, userID, password);

            bool result = false;
            if (AllFormClass.User.Count <= 0)
            {
                result = false;
                MessageBox.Show("Invalid UserID/Password!");
            }
            else if (AllFormClass.User.Count > 0)
            {
                //foreach (user u in AllFormClass.User)
                for (int i = 0; i < AllFormClass.User.Count; i++ )
                {
                    user u = (user)AllFormClass.User[i];
                    if (u.UserID == login.UserID && u.Password == login.Password)
                    {
                        result = true;
                        MessageBox.Show("Welcome! " + u.Name);
                        this.Hide();
                        AllFormClass.we.Show();
                        break;

                    }
                    else
                    {
                        result = false;
                        //break;
                    }
                }
                
            }

            if (result == false)
            {
                MessageBox.Show("Invalid UserID/Password!");
            }


            //MessageBox.Show("Invalid UserID/Password!");
        }

        private void btnRegister_Click(object sender, EventArgs e)
        {
            pnlRegister.Show();
            tbxNewName.Clear();
            tbxNewPassword.Clear();
            tbxNewUserID.Clear();
            tbxNewPasswordComfirmation.Clear();
            tbxNewName.Focus();
        }

        private void btncancel_Click(object sender, EventArgs e)
        {
            
            tbxNewName.Clear();
            tbxNewPassword.Clear();
            tbxNewUserID.Clear();
            tbxNewPasswordComfirmation.Clear();
            pnlRegister.Hide();
            pnlLoginEmpty.Show();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.lbltime.Text = DateTime.Now.ToString();
        }


    }
}
